package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.lang.Result;
import com.example.entity.Post;
import com.example.service.CommentService;
import com.example.service.PostService;
import com.example.vo.PostVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author 吕一明
 * @公众号 码客在线
 */
@Controller
public class PostController extends BaseController {

    @RequestMapping("/category/{id:\\d*}")
    public String category(@PathVariable Long id) {
        Page page = getPage();

        IPage<PostVo> pageData = postService.paging(page, null, id, null, null, "created");
        req.setAttribute("pageData", pageData);
        req.setAttribute("currentCategoryId", id);

        return "post/category";
    }

    @RequestMapping("/post/{id:\\d*}")
    public String view(@PathVariable Long id) {

        QueryWrapper wrapper = new QueryWrapper<Post>()
                .eq(id != null, "p.id", id);
        PostVo vo = postService.selectOne(wrapper);

        IPage commentPage = commentService.paging(getPage(), null, id, "id");

        req.setAttribute("post", vo);
        req.setAttribute("pageData", commentPage);
        req.setAttribute("currentCategoryId", vo.getCategoryId());

        return "post/view";
    }

}
